package com.example.timetable;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.example.timetable.method.AlertManage;
import com.example.timetable.method.Database;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;

@Route(path = "/TimeTable/GoToClassAlert")
public class GoToClassAlert extends AppCompatActivity {

    @BindView(R.id.switch_alert)
    Switch switch_alert;
    @BindView(R.id.switch_sound)
    Switch switch_sound;
    @BindView(R.id.switch_vibrator)
    Switch switch_vibrator;
    @BindView(R.id.radio_minute)
    RadioGroup radio_minute;
    @BindView(R.id.minute1)
    RadioButton minute1;
    @BindView(R.id.minute5)
    RadioButton minute5;
    @BindView(R.id.minute10)
    RadioButton minute10;
    @BindView(R.id.minute15)
    RadioButton minute15;
    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_go_to_class_alert);
        ButterKnife.bind(this);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        preferences = getSharedPreferences("GoToClassAlert", Context.MODE_PRIVATE);
        editor = preferences.edit();
        initView();
    }
    private void initView(){
        boolean alert=preferences.getBoolean("alert",false);
        boolean sound=preferences.getBoolean("sound",false);
        boolean vibrate=preferences.getBoolean("vibrate",false);
        int selectMinute=preferences.getInt("minute",1);
        switch_alert.setChecked(alert);
        switch_sound.setEnabled(alert);
        switch_vibrator.setEnabled(alert);
        minute1.setEnabled(alert);
        minute5.setEnabled(alert);
        minute10.setEnabled(alert);
        minute15.setEnabled(alert);

        switch_sound.setChecked(sound);
        switch_vibrator.setChecked(vibrate);
        switch (selectMinute) {
            case 1:
                minute1.setChecked(true);
                break;
            case 5:
                minute5.setChecked(true);
                break;
            case 10:
                minute10.setChecked(true);
                break;
            case 15:
                minute15.setChecked(true);
                break;
        }
    }
    @OnCheckedChanged(R.id.switch_alert)
    public void OnCheckedChangedAlert(boolean isChecked){
        if(isChecked)
            editor.putBoolean("alert",true);
        else{
            editor.putBoolean("alert",false);
            AlertManage alertManage=new AlertManage();
            alertManage.deleteAlert(this);
        }

        editor.commit();
        initView();
    }
    @OnCheckedChanged(R.id.switch_sound)
    public void OnCheckedChangedSound(boolean isChecked){
        if(isChecked)
            editor.putBoolean("sound",true);
        else
            editor.putBoolean("sound",false);
        editor.commit();
    }
    @OnCheckedChanged(R.id.switch_vibrator)
    public void OnCheckedChangedVibrator(boolean isChecked){
        if(isChecked)
            editor.putBoolean("vibrate",true);
        else
            editor.putBoolean("vibrate",false);
        editor.commit();
    }
    @OnCheckedChanged({R.id.minute1,R.id.minute5,R.id.minute10,R.id.minute15})
    public void OnCheckedChangedMinute(CompoundButton view,boolean isselect){
        switch (view.getId()){
            case R.id.minute1:
                if(isselect)
                    editor.putInt("minute",1);
                break;
            case R.id.minute5:
                if(isselect)
                    editor.putInt("minute",5);
                break;
            case R.id.minute10:
                if(isselect)
                    editor.putInt("minute",10);
                break;
            case R.id.minute15:
                if(isselect)
                    editor.putInt("minute",15);
                break;
        }
        editor.commit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //initAlert();
        AlertManage alertManage=new AlertManage();
        alertManage.initAlert(this,"show");
    }

}
